package com.rainbowelephant.fun.fitness;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class ProfileActivity extends Activity {
	Profile profile = null;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		makeMenu();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_profile, menu);
		return true;
	}

	OnItemClickListener itemClickListen = new OnItemClickListener(){
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			TextView v = (TextView) arg1;
			if(v.getText().equals("New Profile")){
				edit();
			}
			else{
				try{
					load(v.getText()+".profile");
				}catch(Exception e){
					CharSequence text = "Error Loading Profile!";
					Toast toast = Toast.makeText(v.getContext(), text, Toast.LENGTH_LONG);
					toast.show();
					e.printStackTrace();
					System.exit(1);
				}
			}
		}
	};
	
	private void edit() {
		setContentView(R.layout.activity_profile_edit);
		Spinner spin = (Spinner) findViewById(R.id.profile_edit_spinner_gender);
		String [] genders = {"Male","Female"};
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.list_item, genders);
		spin.setAdapter(adapt);
	}

	public void save() throws Exception{
		CharSequence text = "Save Successful!";
		Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		ObjectOutputStream out = new ObjectOutputStream(this.openFileOutput(profile.getName() + ".profile",Context.MODE_PRIVATE));
		out.writeObject(profile);
		out.close();
		toast.show();
	}

	public void saveNew(View v){
		EditText name = (EditText) findViewById(R.id.profile_edit_entry_name);
		EditText heightft = (EditText) findViewById(R.id.profile_edit_entry_heightft);
		EditText heightin = (EditText) findViewById(R.id.profile_edit_entry_heightin);
		EditText weight = (EditText) findViewById(R.id.profile_edit_entry_curweight);
		EditText tarWeight = (EditText) findViewById(R.id.profile_edit_entry_tarweight);
		Spinner gender = (Spinner) findViewById(R.id.profile_edit_spinner_gender);
		Date d = new Date();
		profile = new Profile();
		try{
			profile.setName(name.getText().toString());
			profile.setHeightFeet(Integer.parseInt(heightft.getText().toString()));
			profile.setHeightInches(Integer.parseInt(heightin.getText().toString()));
			profile.setWeight(Integer.parseInt(weight.getText().toString()));
			profile.setIdealWeight(Integer.parseInt(tarWeight.getText().toString()));
			profile.setGender(gender.getSelectedItem().toString());
			profile.setCreationDate(d);
		}catch (NumberFormatException e){
			CharSequence text = "Please fill in all fields!";
			Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
			toast.show();
			return;
		}
		try {
			save();
			updateView();
		} catch (Exception e) {
			CharSequence text = "Profile Creation Failed";
			Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
			toast.show();
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void load(String name)throws Exception{
		ObjectInputStream in = new ObjectInputStream(this.openFileInput(name));
		profile = (Profile) in.readObject();
		in.close();
		updateView();
	}

	public void makeMenu(){
		String [] profiles = this.fileList();
		String [] temp = new String[profiles.length+1];
		for(int i = 0; i < profiles.length; i++){
			temp[i]=profiles[i].substring(0,profiles[i].length() - 8);
		}
		profiles = temp;
		profiles[profiles.length-1] = "New Profile";
		ListView menu = new ListView(this);
		menu.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,profiles));
		menu.setTextFilterEnabled(true);
		menu.setOnItemClickListener(itemClickListen);
		setContentView(menu);
	}

	public void updateView(){
		setContentView(R.layout.activity_profile);

		TextView name = (TextView) findViewById(R.id.profile_value_name);
		TextView curWeight = (TextView) findViewById(R.id.profile_value_curweight);
		TextView tarWeight = (TextView) findViewById(R.id.profile_value_tarweight);
		TextView height = (TextView) findViewById(R.id.profile_value_height);
		TextView create = (TextView) findViewById(R.id.profile_value_creation);

		name.setText(profile.getName());
		curWeight.setText(profile.getWeight()+"");
		tarWeight.setText(profile.getIdealWeight()+"");
		height.setText(profile.getHeightFeet() + "' " + profile.getHeightInches() + "\"");
		create.setText(profile.getCreationDate().toString());
	}
}
